<template>
  <div style="padding: 10px;">
    <div style="margin: 10px 0">
      <el-button type="primary" icon="el-icon-document-add" @click="add">新增</el-button>
    </div>
    <div style="margin: 10px 0">
      <el-input v-model="search" placeholder="请输入内容" style="width:200px" clearable></el-input>
      <el-button type="primary" icon="el-icon-search" style="margin-left: 5px;" @click="load">搜索</el-button>
    </div>

    <el-table
            :data="tableData"
            border
            stripe
            style="width: 100%">
      <el-table-column
              prop="id"
              label="序号">
      </el-table-column>
      <el-table-column
              prop="name"
              label="姓名">
      </el-table-column>
      <el-table-column
              prop="age"
              label="年龄">
      </el-table-column>
      <el-table-column
        prop="email"
        label="邮件">
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-popconfirm
            title="确定删除吗？"
            @confirm="handleDelete(scope.row.id)"
          >
            <el-button
              slot="reference"
              size="mini"
              type="danger">删除</el-button>
          </el-popconfirm>

        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="用户" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off" style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item label="年龄" :label-width="formLabelWidth">
          <el-input v-model="form.age" autocomplete="off" style="width: 100px"></el-input>
        </el-form-item>
        <el-form-item label="邮件" :label-width="formLabelWidth">
          <el-input v-model="form.email" autocomplete="off" style="width: 300px"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>


  import request from "../Utils/request";

  export default {
    name: 'Home',
    components: {

    },
    data() {
      return {
        search: '',
        tableData: [],
        dialogFormVisible: false,
        form: {},
        formLabelWidth: '120px'
      }
    },
    created(){
      this.load()
    },
    methods: {
      //删除
      handleDelete(id) {
        request.delete("http://localhost:9090/user/"+id).then(res => {
          if(res.code == 0){
            this.$message({
              showClose: true,
              message: '恭喜你，删除成功',
              type: 'success'
            });
          }else{
            this.$message({
              showClose: true,
              message: '删除失败',
              type: 'error'
            });
          }
          this.load()

        })

      },
      //修改
      handleEdit(index, row) {
        this.form = JSON.parse(JSON.stringify(row))
        this.dialogFormVisible = true

      },
      load(){
        request.get("http://localhost:9090/user" , {
          params:{
            search: this.search
          }
        } ).then(res => {
          console.log(res)
          this.tableData = res.data
        })
      },
      add(){
        this.dialogFormVisible = true
        this.form = {}
      },
      save(){

        if(this.form.id){  //更新
          request.put("http://localhost:9090/user", this.form).then(res => {
            if(res.code == 0){
              this.$message({
                showClose: true,
                message: '恭喜你，更新成功',
                type: 'success'
              });
            }else{
              this.$message({
                showClose: true,
                message: '更新失败',
                type: 'error'
              });
            }
            this.load()
          })
        } else {           //新增
          request.post("http://localhost:9090/user", this.form).then(res => {
            if(res.code == 0){
              this.$message({
                showClose: true,
                message: '恭喜你，新增成功',
                type: 'success'
              });
            }else{
              this.$message({
                showClose: true,
                message: '新增失败',
                type: 'error'
              });
            }
            this.load()  //刷新
          })
        }
        //alert(JSON.stringify(this.form))
        this.dialogFormVisible = false
      },
    },
  }
</script>
